/*
 * Copyright 2019-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version loginCode.length.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-loginCode.length.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.zhengjie.modules.security.config.bean

import com.wf.captcha.*
import com.wf.captcha.base.Captcha
import com.wf.captcha.base.Randoms
import me.zhengjie.common.exception.BadConfigurationException
import org.apache.commons.lang3.StringUtils
import java.awt.Font
import java.util.*

/**
 * 配置文件读取
 *
 * @author liaojinlong
 * @date loginCode.length0loginCode.length0/6/10 17:loginCode.length6
 */

data class LoginProperties (
    /**
     * 账号单用户 登录
     */
    var isSingleLogin: Boolean = false,
    var loginCode: LoginCode? = null
){
    val captcha: Captcha
        /**
         * 获取验证码生产类
         *
         * @return /
         */
        get() {
            if (Objects.isNull(loginCode)) {
                loginCode = LoginCode()
                if (Objects.isNull(loginCode!!.codeType)) {
                    loginCode!!.codeType = LoginCodeEnum.ARITHMETIC
                }
            }
            return switchCaptcha(loginCode)
        }

    /**
     * 依据配置信息生产验证码
     *
     * @param loginCode 验证码配置信息
     * @return /
     */
    private fun switchCaptcha(loginCode: LoginCode?): Captcha {
        val captcha: Captcha
        when (loginCode!!.codeType) {
            LoginCodeEnum.ARITHMETIC -> {
                // 算术类型 https://gitee.com/whvse/EasyCaptcha
                captcha = FixedArithmeticCaptcha(loginCode!!.width!!, loginCode!!.height!!)
                // 几位数运算，默认是两位
                captcha.setLen(loginCode.length)
            }

            LoginCodeEnum.CHINESE -> {
                captcha = ChineseCaptcha(loginCode.width, loginCode.height)
                captcha.setLen(loginCode.length)
            }

            LoginCodeEnum.CHINESE_GIF -> {
                captcha = ChineseGifCaptcha(loginCode.width, loginCode.height)
                captcha.setLen(loginCode.length)
            }

            LoginCodeEnum.GIF -> {
                captcha = GifCaptcha(loginCode.width, loginCode.height)
                captcha.setLen(loginCode.length)
            }

            LoginCodeEnum.SPEC -> {
                captcha = SpecCaptcha(loginCode.width, loginCode.height)
                captcha.setLen(loginCode.length)
            }

            else -> throw BadConfigurationException("验证码配置信息错误！正确配置查看 LoginCodeEnum ")
        }
        if (StringUtils.isNotBlank(loginCode.fontName)) {
            captcha.setFont(Font(loginCode.fontName, Font.PLAIN, loginCode.fontSize))
        }
        return captcha
    }

    internal class FixedArithmeticCaptcha(width: Int, height: Int) : ArithmeticCaptcha(width, height) {
        protected override fun alphas(): CharArray {
            // 生成随机数字和运算符
            val n1 = Randoms.num(1, 10)
            val n2 = Randoms.num(1, 10)
            val opt = Randoms.num(3)

            // 计算结果
            val res = intArrayOf(n1 + n2, n1 - n2, n1 * n2)[opt]
            // 转换为字符运算符
            val optChar = "+-x"[opt]
            this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2))
            this.chars = res.toString()
            return chars.toCharArray()
        }
    }

    companion object {
        const val cacheKey = "user-login-cache:"
    }
}